#include<iostream>
#include"../Comm/httplib.h"
#include"control.hpp"
#include<string>
#include<signal.h>
using namespace ns_control;
using namespace httplib;
Control* trol_ptr=nullptr;
void Recover(int signo)
{
    trol_ptr->OnlineMachine();
}
int main()
{
    //一、网路路由功能
    //1、请求首页，所有题目列表
    signal(SIGQUIT,Recover);
    Server svr;
    Control trol;
    trol_ptr=&trol;
    svr.Get("/all_question",[&trol](const Request&req,Response&resp){
        std::string html;
        trol.GetQuestionAll(&html);
        resp.set_content(html,"text/html;charset=utf-8");

        // resp.set_content("所有题目列表","text/plain;charset=utf-8");
    });
    //2、请求具体摸到题目的编辑
    svr.Get(R"(/question/(\d+))",[&trol](const Request&req,Response&resp){
        std::string number=req.matches[1];
        std::string html;
        trol.GetQuestionOne(number,&html);
         resp.set_content(html,"text/html;charset=utf-8");

        // resp.set_content("这是具体一道题目的编辑区:"+number,"text/plain;charset=utf-8");
    });
    //3、用户提交的代码的判题（1、测试用例，2、compile_and_run）
    svr.Post(R"(/judge/(\d+))",[&trol](const Request&req,Response&resp){
        std::string number=req.matches[1];
        std::string result_json;
        std::cout<<"number:"<<number<<" body:"<<req.body<<std::endl;
        trol.Judge(number,req.body,&result_json);
        resp.set_content(result_json,"application/json;charset=utf-8");
        // resp.set_content("这是要判断的题目:"+number,"text/plain;charset=utf-8");
    });
    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0",8081);
    return 0;
}